<template>

    <div class="login_container">
        <div class="login_box">
            <h2 align='center'>登录系统</h2>
            <!-- 表单区域 -->
            <el-form ref="loginFormRef" :rules="loginRules" :model="loginForm" class="login_form" label-width="0">
                <!-- 用户名 -->
                <el-form-item prop="username">
                    <el-input type="text" placeholder="用户名" v-model="loginForm.username" prefix-icon="iconfont icon-denglu"></el-input>
                </el-form-item>
                <!-- 密码 -->
                <el-form-item prop="password">
                    <el-input type="password" placeholder="密码" v-model="loginForm.password" prefix-icon="iconfont icon-mima"></el-input>
                </el-form-item>
                <!-- 按钮 -->
                <el-form-item >
                    <el-button class="btl" type="primary" @click="login" size="medium">登录</el-button>    
                </el-form-item>
                <el-form-item >
                    <el-button class="btl" type="text" @click="register" size="medium">没有账号，立即注册</el-button>    
                </el-form-item>
            </el-form>    
        </div>
    </div>
</template>
<script>
export default {
    data(){
        return{
            loginForm:{
                username:"",
                password:"",
            },
            loginRules:{
                username:[
                    { required: true, message: "请输入用户名", trigger: "blur" },
                    { min: 2, max: 10, message: "用户名长度在 2 到 10 个字符", trigger: "blur" }
                ],
                password:[
                    { required: true, message: "请输入密码", trigger: "blur" },
                    { min: 6, max: 12, message: "密码长度为 6~12 位", trigger: "blur" }
                ],
            },
        };
    },
    methods:{
        register(){
            this.$router.push({ path: "/register"});
        },
        //登录验证
        login(){
            //验证校验规则
            this.$refs.loginFormRef.validate(async valid=>{
                if(!valid){
                    return;//验证失败
                }
                const {data:res}=await this.$http.post("login",this.loginForm);//访问后台
                if (res.flag == "ok" ) {
                    window.sessionStorage.setItem('flag','ok'); // session 放置
                    this.$message.success("登陆成功！！！");
                    this.$router.push({ path: "/login"});
                    console.log(res.user);
                }else{
                    this.$message.error("登录失败！！！");
                }
            })
        },
    },
}
</script>
<style lang="less" scoped>
    .login_container{
        background:url(https://z3.ax1x.com/2021/05/24/gje16x.jpg);
        background-color: rgba(9, 164, 216, 0.849);
        position:fixed;
        background-size:100% 100%;
        height:100%;
        width: 100%;
    }
    .login_box{
        background-color:rgba(255, 255,255, 0.4);
        width: 300px;
        height: 300px;
        border-radius: 3px; 
        padding: 20px ;      
        position: absolute;
        left: 50%;
        top:50%;
        transform: translate(-50%,-50%);
    }
    .btl{
        width: 100%;
        font-weight: bold;
    }
</style>


